﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using app.Models;
using AmuTools;

namespace app.Api
{
	public class LandingServer
    {
        private static Dictionary<string, LandingUnit> map = new Dictionary<string, LandingUnit>();

        public static void AddUnit(string key, string token)
        {
            if (IsValid(key, token)) map[key].Update();
            else map[key] = new LandingUnit(key, token);
        }
        public static void UpdateUnit(string key, string token)
        {
            if (IsValid(key, token)) map[key].Update();
        }
        public static void RemoveUnit(string key, string token)
        {
            if (IsValid(key, token)) map.Remove(key);
        }
        public static LANDINGSTATE GetUnitState(string key)
        {
            if (map.ContainsKey(key)) return map[key].CurrentState;
            else return LANDINGSTATE.OFFLINE;
        }
        public static Dictionary<string, object> GetDictionary(string key)
        {
            if (map.ContainsKey(key)) return map[key].Dictionary;
            else return null;
        }
        public static bool HasNewMessage(string key, bool value = false)
        {
            if (map.ContainsKey(key))
            {
                bool temp = map[key].HasNewMessage;
                map[key].HasNewMessage = value;
                return temp;
            }
            else return false;
        }

        public static List<MemberMessage> GetNewMessage(string key, bool value = false)
        {
            List<MemberMessage> temp = new List<MemberMessage>();

            if (map.ContainsKey(key) && map[key].HasNewMessage)
            {
                Cd cd = new Cd();
                cd.And("m_type=0");
                cd.And("status=0");
                cd.And("user_id='{0}'", key);
                temp = G.DB.Get<MemberMessage>(cd.Render()).GetFirstTableList();
                map[key].HasNewMessage = value;
            }
            return temp;
        }

        public static bool IsValid(string key, string token)
        {
            if (map.ContainsKey(key) && token == map[key].Token) return true;
            else return false;
        }
    }

    public class LandingUnit
    {
        public int Intermission { get; set; } // 无操作分钟数，超过视为离开
        private DateTime UpdateTime { get; set; } // 更新时间
        public string Token { get; set; } // 令牌
        private string UserCode { get; set; } // 用户id
        public bool HasNewMessage { get; set; } // 是否有新消息
        public Dictionary<string, object> Dictionary { get; set; }

        public LandingUnit(string user_code, string token, int minutes = 10)
        {
            UserCode = user_code;
            Token = token;
            Intermission = minutes;
            Dictionary = new Dictionary<string, object>();
            Update();
        }

        public void Update()
        {
            UpdateTime = DateTime.Now;
        }

        public LANDINGSTATE CurrentState
        {
            get
            {
                return DateTime.Now.AddMinutes(-Intermission) > UpdateTime ? LANDINGSTATE.LEAVE : LANDINGSTATE.ONLINE;
            }
        }

    }

    public enum LANDINGSTATE
    {
        ONLINE = 1, // 在线
        LEAVE = 2, // 离开
        OFFLINE = 0, // 离线
    }
}